import { message } from 'antd';

export default {
  namespace: 'es',

  state: {
    eapParams: [], // 数据库中的关于和EAP通信的一些参数.
    eapIsConnect: false, // eap是否连接上,默认是false
    eapMsgShowList: [], // eap显示的消息列表
    eapRecipeArgs: [], // EAP下发的配方参数表内容
  },

  effects: {
    *handleEapMsgState({ payload }, { call, put }) {
      const { msg } = payload;
      if (msg.val === 'updateEapMsg') {
        yield put({
          type: 'updateEapMsgShowList',
          payload: msg.eapMsgInfo,
        });
      } else if (msg.val === 'updateEapParamsState') {
        yield put({
          type: 'getEapParams',
        });
        yield put({
          type: 'getEapRecipeParams',
        });
      }else if (msg.val === 'updateEapIsOnline') {
        yield put({
          type: 'updateEapConnect',
          payload:msg.eapIsOnline
        });
      }
    },
    *requestEapRecipeArgs({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'web_request_send_eap_recipe_args_request', [payload]);
      if (response === "ok") {
        message.success("请求EAP工单参数成功!")
      } else {
        message.error("请求EAP高跟单参数失败!" + response, 5);
      }
    },

    *getEapRecipeParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'web_request_get_eap_recipe_params', []);
      yield put({
        type: 'updateEapRecipeParams',
        payload: response,
      });
    },

    *getEapParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'web_request_get_eap_params', []);
      yield put({
        type: 'updateEapParams',
        payload: response,
      });
    },

    *connectEap({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'web_request_connect_eap', []);
      if (response === 'ok') {
        message.success('连接成功!', 3);
        yield put({
          type: 'updateEapConnect',
          payload: true,
        });
      } else {
        message.error('连接失败! 失败原因:' + response, 5);
        yield put({
          type: 'updateEapConnect',
          payload: false,
        });
      }
    },

    *clearMsgShow({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'web_request_clear_msgshow', []);
      if (response === 'ok') {
        message.success('清空成功', 3);
      } else {
        message.error('清空失败,失败原因: ' + response, 3);
      }
    },

    *closeEapConnect({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'web_request_close_eap_connect', []);
      if (response === 'ok') {
        message.success('断开EAP连接成功!', 3);
        yield put({
          type: 'updateEapConnect',
          payload: false,
        });
      } else {
        message.error('断开EAP连接失败! 失败原因:' + response, 3);
      }
    },

    *saveEapParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'es', 'web_request_save_eap_params', [
        payload,
      ]);
      if (response == 'ok') {
        message.success('保存成功', 3);
      } else {
        message.error('保存失败,失败原因: ', response);
      }
      yield put({
        type: 'getEapParams',
      });
    },
  },
  reducers: {
    updateEapRecipeParams(state, action) {
      return {
        ...state,
        eapRecipeArgs: action.payload,
      };
    },
    updateEapConnect(state, action) {
      return {
        ...state,
        eapIsConnect: action.payload,
      };
    },
    updateEapMsgShowList(state, action) {
      return {
        ...state,
        eapMsgShowList: action.payload,
      };
    },
    updateEapParams(state, action) {
      return {
        ...state,
        eapParams: action.payload,
      };
    },
  },
};
